package com.paobuji.aidebao.myapplication.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.activity.BaseActivity;
import com.paobuji.aidebao.myapplication.adapter.MyHelpAdapter;
import com.paobuji.aidebao.myapplication.appinterface.GetInterfaceObjctListener;
import com.paobuji.aidebao.myapplication.bean.GetAllQuestion;
import com.paobuji.aidebao.myapplication.tool.MyIntentTools;
import com.paobuji.aidebao.myapplication.view.CustomTitlebar;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyHelpActivity extends BaseActivity {

    @BindView(R.id.mainTitle)
    CustomTitlebar mainTitle;
    @BindView(R.id.myHelp)
    RecyclerView myHelp;

    MyHelpAdapter myHelpAdapter;
    Context context;
    ArrayList arrayList;
    MyIntentTools myIntentTools;
//    @BindView(R.id.changeData)
//    TextView changeData;
    int page = 0;
    int totalPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_help);
        ButterKnife.bind(this);
        //设置全透明
        StatusBarUtil.setTransparent(this);
        arrayList = new ArrayList();
        context = getApplicationContext();
        myIntentTools = new MyIntentTools();
        initRecycleView(page);

        //标题栏事件监测
        mainTitle.setAction(new CustomTitlebar.TitleBarOnClickListener() {
            @Override
            public void performAction(View view) {
                switch (view.getId()) {
                    case R.id.iv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "左边图片按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.iv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

        myHelp.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false));
    }

    //实例化 RecycleView
    public void initRecycleView(int n) {

        String[] question_array = getResources().getStringArray(R.array.question_array);
        String[] answer_array = getResources().getStringArray(R.array.answer_array);
        final ArrayList<GetAllQuestion.DataBean.QuestionlistBean> questionlistBeans = new ArrayList<>();
        for (int i = 0; i < question_array.length; i++) {
            GetAllQuestion.DataBean.QuestionlistBean questionlistBean = new GetAllQuestion.DataBean.QuestionlistBean(question_array[i],answer_array[i]);
            questionlistBeans.add(questionlistBean);
        }
        if(myHelpAdapter == null){
            myHelpAdapter = new MyHelpAdapter(MyHelpActivity.this, questionlistBeans);
            myHelp.setAdapter(myHelpAdapter);
        }else{
            myHelpAdapter.changeData(questionlistBeans);
        }

        myHelpAdapter.setOnItemClickListener(new MyHelpAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, ArrayList arrayList) {
                Intent intent = new Intent(MyHelpActivity.this, MyQuestionInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("question",  questionlistBeans.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
//        myIntentTools.getAllQuestionList(context, "", n, 5);
//        myIntentTools.setGetInterfaceObjctListener(new GetInterfaceObjctListener() {
//            @Override
//            public void getObjectDate(Object data) {
//                if (((GetAllQuestion) data).getCode() == 0) {
//                    totalPage = ((GetAllQuestion) data).getData().getExtra().getTotalPage();
//                    if(myHelpAdapter == null){
//                        myHelpAdapter = new MyHelpAdapter(MyHelpActivity.this, (ArrayList) ((GetAllQuestion) data).getData().getQuestionlist());
//                        myHelp.setAdapter(myHelpAdapter);
//                    }else{
//                        myHelpAdapter.changeData((ArrayList) ((GetAllQuestion) data).getData().getQuestionlist());
//                    }
//
//                    myHelpAdapter.setOnItemClickListener(new MyHelpAdapter.OnItemClickListener() {
//                        @Override
//                        public void onItemClick(View view, int position, ArrayList arrayList) {
//                            Intent intent = new Intent(MyHelpActivity.this, MyQuestionInfoActivity.class);
//                            Bundle bundle = new Bundle();
//                            bundle.putParcelable("question", ((GetAllQuestion.DataBean.QuestionlistBean) arrayList.get(position)));
//                            intent.putExtras(bundle);
//                            startActivity(intent);
//                        }
//                    });
//
//                } else {
//                    toastLog(R.string.erroneous + "");
//                }
//            }
//        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

//    @OnClick(R.id.changeData)
//    public void onClick() {
//        page++;
//        if(page >= totalPage){
//            toastLog("no more");
//        }else{
//            initRecycleView(page);
//        }
//    }
}
